/**
 * 登录设备 API 接口
 * 提供登录设备的查询、管理等功能
 */
import { http } from "@/utils/core/http"
import { apiWrapper, apiWrapperWithAuth } from "@/utils/core/api-wrapper"

/**
 * 获取当前用户的登录设备列表
 * @param {Object} params - 查询参数
 * @returns {Promise}
 */
export function getLoginDevices(params = {}) {
  return apiWrapperWithAuth((userId) =>
    http.get(`/loginDevices/${userId}`, {
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    })
  )
}

/**
 * 记录登录设备信息
 * @param {Object} data - 登录设备数据
 * @returns {Promise}
 */
export function recordLoginDevice(data) {
  return apiWrapper(() =>
    http.post('/loginDevices/record', {
      userId: data.userId,
      deviceId: data.deviceId,
      deviceName: data.deviceName || '',
      deviceType: data.deviceType || 'PC',
      browser: data.browser || '',
      os: data.os || '',
      loginMethod: data.loginMethod,
      ipAddress: data.ipAddress,
      location: data.location || '',
      sessionToken: data.sessionToken || ''
    })
  )
}

/**
 * 登出指定设备
 * @param {number} deviceId - 设备记录ID
 * @returns {Promise}
 */
export function logoutDevice(deviceId) {
  return apiWrapperWithAuth((userId) => http.post(`/loginDevices/${userId}/logout/${deviceId}`))
}

/**
 * 登出所有其他设备
 * @returns {Promise}
 */
export function logoutAllOtherDevices() {
  return apiWrapperWithAuth((userId) => http.post(`/loginDevices/${userId}/logoutAll`))
}

/**
 * 删除登录设备记录
 * @param {number} deviceId - 设备记录ID
 * @returns {Promise}
 */
export function deleteLoginDevice(deviceId) {
  return apiWrapperWithAuth((userId) => http.delete(`/loginDevices/${userId}/${deviceId}`))
}

/**
 * 更新设备名称
 * @param {number} deviceId - 设备记录ID
 * @param {string} deviceName - 新的设备名称
 * @returns {Promise}
 */
export function updateDeviceName(deviceId, deviceName) {
  return apiWrapperWithAuth((userId) =>
    http.put(`/loginDevices/${userId}/${deviceId}/name`, { deviceName })
  )
}

/**
 * 获取当前设备信息
 * @returns {Promise}
 */
export function getCurrentDevice() {
  return apiWrapperWithAuth((userId) => http.get(`/loginDevices/${userId}/current`))
}
